package org.iplugin.version.handler;

import org.iplugin.version.IVersionAbstractUpdateMojo;
import org.iplugin.version.model.IModifiedPomVersion;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.model.Model;
import org.apache.maven.model.Parent;

/**
 * 　　* @Description: maven parent version update handler
 * 　　* @author shangyouzhi1
 * 　　* @date 2022/11/27 16:35
 */
public class IVersionUpdateParentHandler extends IVersionUpdateAbstractHandler {

    public IVersionUpdateParentHandler() {
        this.setNextHandler(new IVersionUpdateDependencyHandler());
    }

    @Override
    public void updateVersion(IModifiedPomVersion modifiedPomVersion) {
        Model model = modifiedPomVersion.getModel();
        Parent parent = model.getParent();
        if (parent != null) {
            String oldVersion = parent.getVersion();
            String newVersion = IVersionAbstractUpdateMojo.newVersionMap.get(parent.getGroupId() + ":" + parent.getArtifactId());
            if (StringUtils.isNotBlank(newVersion) && !StringUtils.equals(oldVersion, newVersion)) {
                if (StringUtils.isNotBlank(oldVersion)) {
                    this.getLog().info("更新parent version：" + parent.getGroupId() + ":" + parent.getArtifactId() + ",更新前：" + oldVersion + ",更新后:" + newVersion);
                    parent.setVersion(newVersion);
                    modifiedPomVersion.modifiedVersion();
                }
            }
        }
        if (getNextHandler() != null) {
            getNextHandler().updateVersion(modifiedPomVersion);
        }
    }
}
